package indi.imooc.datastructure.queue.impl;

import indi.imooc.datastructure.queue.Queue;

/**
 * 循环队列
 * front == tail 队列为空
 * (tail + 1) % data.length == front 队列为满
 * 不适用size变量
 *  1.getSize() 方法
 *  2.resize() 方法中需要使用临时变量size
 * @param <E>
 */
public class LoopQueueNoSize<E> implements Queue<E> {

    private E[] data;
    private int front,tail;

    public LoopQueueNoSize(){
        this(10);
    }

    public LoopQueueNoSize(int capacity){
        data = (E[]) new Object[capacity + 1];
        front = 0;
        tail = 0;
    }

    @Override
    public int getSize() {
        // method 1
        return tail >= front ? tail - front  : data.length - front + tail;
        // method 2
//        return (tail - front + data.length) % data.length;
    }

    public int getCapacity(){
        return data.length - 1;
    }

    @Override
    public boolean isEmpty() {
        return front == tail;
    }



    @Override
    public void enqueue(E e) {
        if((tail + 1) % data.length == front){
            resize(2 * getCapacity());
        }
        data[tail] = e;
        tail = (tail + 1) % data.length;
    }

    @Override
    public E dequeue() {
        if(isEmpty()){
            throw new IllegalArgumentException("LoopQueue is empty");
        }
        E e = data[front];
        data[front] = null;
        front = (front + 1 ) % data.length;

        if (getSize() == getCapacity() / 4 && getCapacity() / 2 != 0){
            resize(getCapacity() / 2);
        }
        return e;
    }

    @Override
    public E getFront() {
        if(isEmpty()){
            throw new IllegalArgumentException("LoopQueue is empty");
        }
        return data[front];
    }

    /**
     * resize时,数组中元素重新排列,front作为第一个元素
     * @param newCapacity
     */
    private void resize(int newCapacity) {
        E[] newArray = (E[]) new Object[newCapacity + 1];
        // front > tail 时, size = data.length + tail - front,data重新赋值后length发生变化,getSize()也会变化,所以使用变量表示
        int size = getSize();
        // v1.0
//        for (int i = 0; i < getSize(); i++) {
//            newArray[i] = data[(i + front) % data.length];
//        }

        // v2.0
        for (int i = front, index = 0; i != tail ; i = (i + 1) % data.length) {
            newArray[index++] = data[i];
        }

        data = newArray;
        tail = size;
        front = 0;
    }

    @Override
    public String toString(){
        StringBuilder res = new StringBuilder();
        res.append(String.format("LoopQueue : size = %d , capacity = %d\n", getSize(), getCapacity()));
        res.append("front [ ");
        // v1.0
        for (int i = front; i != tail; i = (i + 1) % data.length) {
            res.append(data[i]);
            if ((i + 1) % data.length != tail){
                res.append(", ");
            }
        }

        // v2.0
//        for (int i = 0; i < getSize(); i++) {
//            res.append(data[(i + front) % data.length]);
////            if((i + front + 1 ) % data.length != tail){
//            if(i != getSize() - 1){
//                res.append(", ");
//            }
//        }
        res.append(" ] tail");
        return res.toString();
    }

    public static void main(String[] args) {
        LoopQueueNoSize<Integer> loopQueue = new LoopQueueNoSize<>();
        for (int i = 0; i < 16; i++) {
            System.out.println(i);
            loopQueue.enqueue(i);
            System.out.println(loopQueue.toString());
            if (i % 3 == 2){
                loopQueue.dequeue();
                System.out.println( loopQueue.toString());
            }
        }

        System.out.println(loopQueue.toString());
        for (int i = 0; i < 12; i++) {
            loopQueue.dequeue();
            System.out.println(loopQueue.toString());
        }

    }
}
